package com.fxc.crowd.mvc.interceptor;

import com.fxc.crowd.constant.CrowdConstant;
import com.fxc.crowd.entity.po.AdminPO;
import com.fxc.crowd.exception.AccessForbiddenException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

        // 1.获取request对象获取session异常
        HttpSession session = request.getSession();

        // 2.尝试从session域中获取Admin对象
        AdminPO adminPO = (AdminPO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);

        // 3.判断Admin对象是否为空
        if (adminPO == null) {

            // 4.抛出异常
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }

        // 5.如果admin不为null则放行
        return true;
    }

}
